package lab3;

/**
 * 版本2.0 习题类
 * 管理单个算术表达式习题，包含用户答案提交和评分功能
 * 支持ArithmeticExpression和AbstractArithmeticExpression
 */
public class Exercise {
    private ArithmeticExpression expression;             // 版本1.0算术表达式
    private AbstractArithmeticExpression abstractExpr;   // 版本2.0抽象算术表达式
    private Double userAnswer;                           // 用户答案
    private boolean isAnswered;                          // 是否已回答
    private boolean isCorrect;                           // 答案是否正确
    private static final double EPSILON = 0.001;          // 浮点数比较的精度

    /**
     * 构造函数 - 接受版本1.0的表达式
     * @param expression 算术表达式对象
     */
    public Exercise(ArithmeticExpression expression) {
        this.expression = expression;
        this.abstractExpr = null;
        this.userAnswer = null;
        this.isAnswered = false;
        this.isCorrect = false;
    }

    /**
     * 构造函数 - 接受版本2.0的抽象表达式
     * @param abstractExpr 抽象算术表达式对象
     */
    public Exercise(AbstractArithmeticExpression abstractExpr) {
        this.expression = null;
        this.abstractExpr = abstractExpr;
        this.userAnswer = null;
        this.isAnswered = false;
        this.isCorrect = false;
    }

    /**
     * 构造函数 - 使用操作数和运算符
     * @param operand1 第一个操作数
     * @param operand2 第二个操作数
     * @param operator 运算符
     */
    public Exercise(double operand1, double operand2, char operator) {
        this.expression = new ArithmeticExpression(operand1, operand2, operator);
        this.abstractExpr = null;
        this.userAnswer = null;
        this.isAnswered = false;
        this.isCorrect = false;
    }

    /**
     * 提交用户答案
     * @param answer 用户提交的答案
     * @return 答案是否正确
     */
    public boolean submitAnswer(double answer) {
        this.userAnswer = answer;
        this.isAnswered = true;
        
        // 比较用户答案和正确答案（考虑浮点数精度）
        // 使用getCorrectAnswer()方法处理两种不同类型的表达式
        this.isCorrect = Math.abs(answer - getCorrectAnswer()) < EPSILON;
        return this.isCorrect;
    }

    /**
     * 获取正确答案
     * @return 算术表达式的计算结果
     */
    public double getCorrectAnswer() {
        if (expression != null) {
            return expression.getResult();
        } else if (abstractExpr != null) {
            return abstractExpr.calculate();
        }
        throw new IllegalStateException("表达式未初始化");
    }

    /**
     * 获取用户答案
     * @return 用户提交的答案，如果未提交则返回null
     */
    public Double getUserAnswer() {
        return userAnswer;
    }

    /**
     * 检查习题是否已回答
     * @return 是否已回答
     */
    public boolean isAnswered() {
        return isAnswered;
    }

    /**
     * 检查答案是否正确
     * @return 答案是否正确
     */
    public boolean isCorrect() {
        return isCorrect;
    }

    /**
     * 获取算术表达式
     * @return 算术表达式对象
     */
    public ArithmeticExpression getExpression() {
        return expression;
    }

    /**
     * 获取抽象算术表达式
     * @return 抽象算术表达式对象
     */
    public AbstractArithmeticExpression getAbstractExpression() {
        return abstractExpr;
    }

    /**
     * 重置习题状态
     */
    public void reset() {
        this.userAnswer = null;
        this.isAnswered = false;
        this.isCorrect = false;
    }

    /**
     * 返回习题的字符串表示
     * @return 习题字符串
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        
        // 根据表达式类型构建字符串
        if (expression != null) {
            String exprStr = expression.toString();
            if (exprStr != null && exprStr.contains(" =")) {
                sb.append(exprStr.split(" =")[0]);
            } else {
                sb.append(exprStr);
            }
        } else if (abstractExpr != null) {
            sb.append(String.format("%.2f %c %.2f", 
                                   abstractExpr.getOperand1(), 
                                   abstractExpr.getOperatorSymbol(), 
                                   abstractExpr.getOperand2()));
        } else {
            sb.append("[未初始化的表达式]");
        }
        
        if (isAnswered) {
            sb.append(" 用户答案: ").append(userAnswer);
            sb.append(" 结果: ").append(isCorrect ? "正确" : "错误");
            if (!isCorrect && userAnswer != null) {
                try {
                    sb.append(" (正确答案: ").append(String.format("%.2f", getCorrectAnswer())).append(")");
                } catch (Exception e) {
                    sb.append(" (无法获取正确答案)");
                }
            }
        }
        
        return sb.toString();
    }
}